﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace MouldingAndMillwork.SSIS
{
    public partial class SSISUtility
    {
        #region CLASS: ByteArray
        public class ByteArray
        {
            #region Integrals To Byte Array Extensions
            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(sbyte value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(short value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(ushort value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(int value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(uint value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(double value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(long value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(ulong value)
            {

                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }

            /// <summary>
            /// Integer To Byte Array
            /// </summary>
            /// <param name="Int">Origonal Integer</param>
            /// <param name="Reversed">To Reverse Integer Bytes Set This To True, Else Leave False</param>
            /// <returns>Byte Array Representation Of Integer</returns>
            public static byte[] ToArray(decimal value)
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(stream))
                    {
                        writer.Write(value);
                        byte[] Bytes = stream.ToArray();
                        stream.Close();
                        stream.Dispose();
                        return Bytes;
                    }
                }
            }
            #endregion

            #region Byte Array Appending
            /// <summary>
            /// Append Byte To End Of Byte Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, byte value)
            {
                System.Array.Resize<byte>(ref array, array.Length + 1);
                array[array.Length - 1] = value;
            }

            /// <summary>
            /// Append Bytes To End Of Byte Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, byte[] value)
            {
                System.Array.Resize<byte>(ref array, array.Length + value.Length);
                System.Array.Copy(value, 0, array, array.Length - value.Length, value.Length);
            }

            /// <summary>
            /// Append SByte Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, sbyte value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// <summary>
            /// Append Short Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, short value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// <summary>
            /// Append UShort Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, ushort value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// Append Integer Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, int value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// Append Long Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, long value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// <summary>
            /// Append UInt Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, uint value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// <summary>
            /// Append Double Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, double value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// <summary>
            /// Append Decimal Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, decimal value)
            {
                ByteArray.Append(ref array, ByteArray.ToArray(value));
            }

            /// <summary>
            /// Append Char Value Bytes To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            public static void Append(ref byte[] array, char value, Encoding Encoding)
            {
                ByteArray.Append(ref array, Encoding.GetBytes(new char[] { value }));
            }

            /// <summary>
            /// Append String Bytes From Encoding To Array
            /// </summary>
            /// <param name="Array">Origonal Value</param>
            /// <param name="Value">Value To Append</param>
            /// <param name="Encoding">Encoding To Use</param>
            public static void Append(ref byte[] array, string value, System.Text.Encoding Encoding)
            {
                ByteArray.Append(ref array, Encoding.GetBytes(value));
            }
            #endregion

        }
        #endregion
    }
}
